/*
 * @(#)DateUtil.java
 * 
 * Project: BUS
 * 
 * Modify Information:
 * =============================================================================
 * Author           Date             Description 
 * HZ SDC          2004/08/10        first  release 
 * Jenny Li        2005/11/08        add parameter TYPE_DATETIMEMIL
 *                                   modify method getDateTime
 * Jenny Li        2005/11/29        add parameter STYLE_FORMAT_FOR_CUPS
 *                                   add method formateDateForCUPS
 *                                   modify method getDateTime
 * Jenny Li        2006/03/28        add method getNext7Day    
 * Jenny Li        2006/06/21        add method getSpecDate,splitSpecChar,getSpecDay                              
 * ---------- --------------------------------------------------- 
 * BJ Tech Dept 2001/12/09 Implement Programming Spec V1.0
 * 
 * Copyright Notice:
 * =============================================================================
 * Copyright (c) 2001-2005 Beijing HiTRUST Technology Co., Ltd. 1808 Room,
 * Science & Technology Building, No. 9 South Street, Zhong Guan Cun, Haidian
 * District, Beijing ,100081, China All rights reserved.
 * 
 * This software is the confidential and proprietary information of Beijing
 * HiTRUST Technology Co., Ltd. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with HiTRUST.
 * 
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.util.date;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.util.exception.GSException;

public class DateUtil {

    /**
     * 日期类型
     */
    public static final String TYPE_DATE = "D";

    /**
     * 时间类型
     */
    public static final String TYPE_TIME = "T";

    /**
     * 日期时间类型
     */
    public static final String TYPE_DATETIME = "DT";
    /**
     * 毫秒级时间类型
     */
    public static final String TYPE_MILLTIME = "TM";
    /**
     * 月日类型
     */
    public static final String TYPE_MD = "MD";

    /**
     * 日期时分秒毫秒类型
     */
    public static final String TYPE_DATETIMEMIL = "DTM";

    /**
     * XML格式
     */
    public static final String STYLE_XML = "X"; // XML�类型

    // CCYY-MM-DDThh:mm:ss+hh:ss
    /**
     * 公元记风格
     */
    public static final String STYLE_AD = "AD"; // �CCYYMMDDhhmmss

    /**
     * 两位年公元记风格
     */
    public static final String STYLE_AD2 = "AD2"; // �YYMMDDhhmmss

    /**
     * TW
     */
    public static final String STYLE_ROC = "R"; // �YYYMMDDhhmmss

    /**
     * CCYY-MM-DD标准格式化日期
     */
    public static final String STYLE_FORMAT = "F"; // ��� CCYY-MM-DD

    // hh:mm:ss
    /**
     * CCYY/MM/DD格式化日期
     */
    public static final String STYLE_FORMAT_FOR_USER = "FU"; // 
    /**
     * CCYYMMDD hh:mm:ss格式化日期
     */
    public static final String STYLE_FORMAT_FOR_CUPS = "CUPS"; // CCYYMMDD hh:mm:ss���

    // CCYY/MM/DD
    // hh:mm:ss

    public DateUtil() {
    }

    /**
     * 填充字符串
     * 
     * @param aSrcString:
     *            传入字符串
     * @param aLen:
     *            字符串长度
     * @return 用右接空格方式补足规定长度的字符串
     * @throws Exception
     */
    public synchronized static String padding(String aSrcString, int aLen) throws Exception {
        if (aSrcString == null) {
            throw new Exception("padding():�Ǥ传入参数是null!");
        }
        String tDesString = null;
        aSrcString = cropping(aSrcString, aLen);
        int tSrcLen = aSrcString.getBytes().length;
        tDesString = aSrcString;
        for (int i = 0; i < (aLen - tSrcLen); i++) {
            tDesString = tDesString + " ";
        }
        return tDesString;
    }

    /**
     * 填充长整型数字字符串
     * 
     * @param aSrcLong:
     *            传入的数字
     * @param aLen:
     *            字符串长度
     * @return 左接0方式补足规定长度的数字字符串
     * @throws Exception
     */
    public synchronized static String padding(long aSrcLong, int aLen) throws Exception {
        String tDesString = null;
        String tSrcString = String.valueOf(aSrcLong);
        tSrcString = cropping(tSrcString, aLen);
        int tSrcLen = tSrcString.length();
        tDesString = tSrcString;
        for (int i = 0; i < (aLen - tSrcLen); i++) {
            tDesString = "0" + tDesString;
        }
        return tDesString;
    }

    /**
     * 填充整型数字字符串
     * 
     * @param aSrcInteger:
     *            传入的数字
     * @param aLen:
     *            字符串长度
     * @return 左接0方式补足规定长度的数字字符串
     * @throws Exception
     */
    public synchronized static String padding(int aSrcInteger, int aLen) throws Exception {
        String tDesString = null;
        String tSrcString = String.valueOf(aSrcInteger);
        tSrcString = cropping(tSrcString, aLen);
        int tSrcLen = tSrcString.length();
        tDesString = tSrcString;
        for (int i = 0; i < (aLen - tSrcLen); i++) {
            tDesString = "0" + tDesString;
        }
        return tDesString;
    }

    /**
     * 返回指定长度的字符串
     * 
     * @param aSrcString
     *            传入的字符串
     * @param aMaxLen
     *            字符串最大长度
     * @return 截取规定的最大长度的字符串
     * @throws Exception
     */
    public synchronized static String cropping(String aSrcString, int aMaxLen) throws Exception {
        if (aSrcString == null) {
            throw new Exception("cropping():�Ǥ传入参数是null!");
        }
        String tDesString = null;
        byte[] tDesBytes = aSrcString.getBytes();
        if (tDesBytes.length > aMaxLen) {
            byte[] tTmpBytes = cropping(tDesBytes, aMaxLen);
            tDesBytes = tTmpBytes;
        }
        tDesString = new String(tDesBytes);
        return tDesString;
    }

    /**
     * 返回指定长度的Byte Array
     * 
     * @param aSrcBytes
     *            传入的Byte Array;
     * @param aMaxLen
     *            最大长度
     * @return 截取规定的最大长度的Byte Array
     * @throws Exception
     */
    public synchronized static byte[] cropping(byte[] aSrcBytes, int aMaxLen) throws Exception {
        if (aSrcBytes == null) {
            throw new Exception("cropping():传入参数是null!");
        }
        byte[] tDesBytes = aSrcBytes;
        if (aSrcBytes.length > aMaxLen) {
            for (int i = 0; i < aMaxLen; i++) {
                if (aSrcBytes[i] < 0) {
                    i++;
                }
                if (i == aMaxLen) {
                    aMaxLen = aMaxLen - 1;
                }
            }
            byte[] tTmpBytes = new byte[aMaxLen];
            System.arraycopy(aSrcBytes, 0, tTmpBytes, 0, aMaxLen);
            tDesBytes = tTmpBytes;
        }
        return tDesBytes;
    }

    /**
     * 获取当前时间并格式化
     * 
     * @param aType:
     *            "D"����, "T"��ɶ�, "DT"
     * @param aStyle:
     *            "X"�, "AD", "R", "F"
     * @return
     */
    public synchronized static String getCurrentTime(String aType, String aStyle)  {
        String tDateTime  = "";
        try{
            Calendar tCalendar = Calendar.getInstance();
            tDateTime = getDateTime(tCalendar, aType, aStyle);
        }catch(Exception e){
            e.printStackTrace();
        }
        return tDateTime;
    }

    /**
     * @param aCalendar
     * @param aType:
     *            "D" ==> 日期����, "T" ==>时间 ��ɶ�, "DT" ==> 日期时间
     * @param aStyle:
     *            "X" ==> XML�, "AD" ==>公元, "R" ==>'TW', "F" ==>格式化日期
     * @return
     */
    public synchronized static String getDateTime(Calendar aCalendar, String aType, String aStyle) throws Exception {
        String tMyDateTime = "";
        if (aType == null || aType.equals("")) {
            aType = TYPE_DATETIME;
        }
        if (aStyle == null || aStyle.equals("")) {
            aStyle = STYLE_AD;
        }
        String tYear, tMonth, tDay, tHour, tMin, tSec, tMiSec;
        if (aStyle.equals(STYLE_ROC)) {
            tYear = padding(aCalendar.get(Calendar.YEAR) - 1911, 2);
        } else if (aStyle.equals(STYLE_AD2)) {
            tYear = Integer.toString(aCalendar.get(Calendar.YEAR)).substring(2, 4);
        } else {
            tYear = padding(aCalendar.get(Calendar.YEAR), 4);
        }
        tMonth = padding(aCalendar.get(Calendar.MONTH) + 1, 2);
        tDay = padding(aCalendar.get(Calendar.DATE), 2);
        tHour = padding(aCalendar.get(Calendar.HOUR_OF_DAY), 2);
        tMin = padding(aCalendar.get(Calendar.MINUTE), 2);
        tSec = padding(aCalendar.get(Calendar.SECOND), 2);
        tMiSec = padding(aCalendar.get(Calendar.MILLISECOND), 3);

        if (aType.equals(TYPE_DATE) || aType.equals(TYPE_DATETIME) || aType.equals(TYPE_DATETIMEMIL)) {
            tMyDateTime = tYear + tMonth + tDay;

        } else if (aType.equals(TYPE_MD)) {
            tMyDateTime = tMonth + tDay;
        }
        if (aType.equals(TYPE_TIME) || aType.equals(TYPE_DATETIME) || aType.equals(TYPE_DATETIMEMIL)
                || aType.equals(TYPE_MILLTIME)) {
            tMyDateTime = tMyDateTime + tHour + tMin + tSec;
            // }
        }
        if (aType.equals(TYPE_DATETIMEMIL) || aType.equals(TYPE_MILLTIME)) {
            tMyDateTime = tMyDateTime + tMiSec;
        }
        if (aStyle.equals(STYLE_FORMAT)) {
            tMyDateTime = formateDateTime(tMyDateTime);
        } else if (aStyle.equals(STYLE_XML)) {
            tMyDateTime = strTime2XMLTime(tMyDateTime);
        } else if (aStyle.equals(STYLE_FORMAT_FOR_USER)) {
            tMyDateTime = formateDateTimeForUser(tMyDateTime);
        }else if (aStyle.equals(STYLE_FORMAT_FOR_CUPS)){
            tMyDateTime = formateDateForCUPS(tMyDateTime);
        }

        return tMyDateTime;
    }
    public static String formateDateForCUPS(String aMyDateTime) throws Exception {
        StringBuffer tRtnDateTime = new StringBuffer();
        if (aMyDateTime.length() != 14) {
            throw new Exception("时期长度不正确�!");
        }
        tRtnDateTime = tRtnDateTime.append(aMyDateTime.substring(0,8)).append(" ")
                        .append(aMyDateTime.substring(8, 10)).append(":").append(aMyDateTime.substring(10, 12))
                        .append(":").append(aMyDateTime.substring(12, 14));
        return tRtnDateTime.toString();
    }
    /**
     * 格式化日期
     * 
     * @param aMyDateTime
     * @return ex. 20011217 ==> 2001/12/17 ex. 151617 ==> 15:16:17 ex.
     *         20011217151617 ==> 2001/12/17 15:16:17
     */
    public synchronized static String formateDateTimeForUser(String aMyDateTime) throws Exception {
        String tRtnDateTime = "";
        if (aMyDateTime == null) {
            return tRtnDateTime;
        }
        if (aMyDateTime.length() == 8 || aMyDateTime.length() == 14) {
            tRtnDateTime = aMyDateTime.substring(0, 4) + "/" + aMyDateTime.substring(4, 6) + "/"
                    + aMyDateTime.substring(6, 8);
            if (aMyDateTime.length() == 14) {
                tRtnDateTime = tRtnDateTime + " ";
                aMyDateTime = aMyDateTime.substring(8);
            }
        }
        if (aMyDateTime.length() == 6) {
            tRtnDateTime = tRtnDateTime + aMyDateTime.substring(0, 2) + ":" + aMyDateTime.substring(2, 4) + ":"
                    + aMyDateTime.substring(4, 6);
        }
        return tRtnDateTime;
    }

    /**
     * 格式化日期
     * 
     * @param aMyDateTime
     * @return ex. 20011217 ==> 2001-12-17 ex. 151617 ==> 15:16:17 ex.
     *         20011217151617 ==> 2001-12-17 15:16:17
     */
    public synchronized static String formateDateTime(String aMyDateTime) throws Exception {
        String tRtnDateTime = "";
        if (aMyDateTime.length() == 8 || aMyDateTime.length() == 14) {
            tRtnDateTime = aMyDateTime.substring(0, 4) + "-" + aMyDateTime.substring(4, 6) + "-"
                    + aMyDateTime.substring(6, 8);
            if (aMyDateTime.length() == 14) {
                tRtnDateTime = tRtnDateTime + " ";
                aMyDateTime = aMyDateTime.substring(8);
            }
        }
        if (aMyDateTime.length() == 6) {
            tRtnDateTime = tRtnDateTime + aMyDateTime.substring(0, 2) + ":" + aMyDateTime.substring(2, 4) + ":"
                    + aMyDateTime.substring(4, 6);
        }
        return tRtnDateTime;
    }

    /**
     * 格式化转为非格式化日期
     * 
     * @param aMyDateTime
     * @return ex. 2001/12/17 ==> 20011217 ex. 2001-12-17 ==> 20011217 ex.
     *         16:15:17 ==> 161517 ex. 2001-11-12T15:16:17+08:00 ==>
     *         20011112151617
     */
    public synchronized static String revertDateTime(String aMyDateTime) throws Exception {
        String tRtnDateTime = "";
        if (aMyDateTime.length() == 10 || aMyDateTime.length() == 19) {
            tRtnDateTime = aMyDateTime.substring(0, 4) + aMyDateTime.substring(5, 7) + aMyDateTime.substring(8, 10);
            if (aMyDateTime.length() == 19) {
                aMyDateTime = aMyDateTime.substring(11);
            }
        }
        if (aMyDateTime.length() == 8) {
            tRtnDateTime = tRtnDateTime + aMyDateTime.substring(0, 2) + aMyDateTime.substring(3, 5)
                    + aMyDateTime.substring(6, 8);
        }
        return tRtnDateTime;
    }

    /**
     * 格式化日期
     * 
     * @param aXmlTime
     * @return ex. 20011112 ==> 2001-11-12 ex. 20011112151617 ==>
     *         2001-11-12T15:16:17+08:00
     */
    public synchronized static String strTime2XMLTime(String aXmlTime) throws Exception {
        String tRtnDateTime = "";
        // String timezone =
        // Calendar.getInstance().getTimeZone().getDisplayName().substring(3);
        String tTimeZone = "+08:00";
        if (aXmlTime == null || (aXmlTime.length() != 14 && aXmlTime.length() != 6 && aXmlTime.length() != 8)) {
            throw new Exception("strTime2XMLTime():传入参数为空或长度不符!");
        }
        if (aXmlTime.length() == 6 || aXmlTime.length() == 8) {
            tRtnDateTime = formateDateTime(aXmlTime);
        } else if (aXmlTime.length() == 14) {
            tRtnDateTime = formateDateTime(aXmlTime.substring(0, 8)) + "T" + formateDateTime(aXmlTime.substring(8, 14))
                    + tTimeZone;
        }
        return tRtnDateTime;
    }

    /**
     * 格式化日期
     * 
     * @param aStrTime
     * @return ex. 2001-12-21 or 16:35:45 or 25�Xxml�
     */
    public synchronized static String xmlTime2StrTime(String aStrTime) throws Exception {
        String tRtnDateTime = "";
        if (aStrTime == null || (aStrTime.length() != 25 && aStrTime.length() != 10 && aStrTime.length() != 8)) {
            throw new Exception("xmlTime2StrTime():�Ǥ传入参数为空或长度不符!");
        }
        if (aStrTime.length() == 10 || aStrTime.length() == 8) {
            tRtnDateTime = revertDateTime(aStrTime);
        } else if (aStrTime.length() == 25) {
            tRtnDateTime = revertDateTime(aStrTime.substring(0, 19));
        }
        return tRtnDateTime;
    }

    /**
     * 检查字符串是否是数字
     * 
     * @param aIntString
     * @return
     */
    public synchronized static boolean checkIntIsValid(String aIntString) {
        boolean tIsValid = false;
        try {
            Long.parseLong(aIntString);
            tIsValid = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tIsValid;
    }

    /**
     * 判断日期是否被正确的格式化
     * 
     * @param aDateString
     *            合法日期例子: 2001-11-12 or 15:16:17 or 2001-12-17T15:16:17+08:00
     *            合法日期例子: 20011112 or 151617 or 20011112151617 合法日期例子:
     *            2001-11-12 15:16:17
     * @return �
     */
    public synchronized static boolean checkDateIsValid(String aDateString) {
        boolean tIsValid = false;
        try {
            if (aDateString.length() == 25 || aDateString.length() == 10
                    || (aDateString.length() == 8 && aDateString.indexOf(":") == 2)) {
                aDateString = xmlTime2StrTime(aDateString);
            } else if (aDateString.length() == 19) {
                aDateString = revertDateTime(aDateString);
            }
            // Calendar aCalendar = string2Calendar(aDateString);
            tIsValid = true;
        } catch (Exception e) {
        }
        return tIsValid;
    }

    /**
     * 计算给定日期加给定天数后的日期
     * 
     * @param aOrgDate:
     *            日期
     * @param aDayCnt:
     *            天数
     * @return 计算后的日期
     * @throws Exception
     */
    public synchronized static String countDate(String aOrgDate, int aDayCnt) throws Exception {
        String tMyDateLine = null;
        String tMyType = null;
        Calendar tCalendar = string2Calendar(aOrgDate);
        tCalendar.add(Calendar.DATE, aDayCnt);
        if (aOrgDate.length() == 8) {
            tMyType = TYPE_DATE;
        } else if (aOrgDate.length() == 14) {
            tMyType = TYPE_DATETIME;
        }
        tMyDateLine = getDateTime(tCalendar, tMyType, STYLE_AD);
        return tMyDateLine;
    }

    /**
     * 将日期字符串转化为Calendar对象
     * 
     * @param aDateString
     *            ex. 20011112 or 20011112151617 or 151617
     * @return Calendar�
     */
    public synchronized static Calendar string2Calendar(String aDateString) throws Exception {
        int tYear = 0, tMonth = 0, tDate = 0, tHour = 0, tMin = 0, tSec = 0, tMyLen = 0;
        if (aDateString == null) {
            throw new Exception("string2Calendar():�ǤJ��nɶ���null!");
        }
        tMyLen = aDateString.length();
        if (tMyLen == 8 || tMyLen == 14) {
            tYear = Integer.parseInt(aDateString.substring(0, 4));
            tMonth = Integer.parseInt(aDateString.substring(4, 6)) - 1;
            tDate = Integer.parseInt(aDateString.substring(6, 8));
            if (tMyLen == 14) {
                aDateString = aDateString.substring(8);
            }
        }

        if (aDateString.length() == 6) {
            tHour = Integer.parseInt(aDateString.substring(0, 2));
            tMin = Integer.parseInt(aDateString.substring(2, 4));
            tSec = Integer.parseInt(aDateString.substring(4, 6));
        }

        Calendar tCalendarObj = Calendar.getInstance();
        try {
            if (tMyLen == 8) {
                tCalendarObj.set(tYear, tMonth, tDate);
                if (tYear != tCalendarObj.get(Calendar.YEAR) || tMonth != (tCalendarObj.get(Calendar.MONTH))
                        || tDate != tCalendarObj.get(Calendar.DATE)) {
                    throw new Exception("���~!");
                }
            } else if (tMyLen == 6) {
                if (tHour < 0 || tHour >= 24 || tMin < 0 || tMin >= 60 || tSec < 0 || tSec >= 60) {
                    throw new Exception("�ɶ���~!");
                }
            } else if (tMyLen == 14) {
                tCalendarObj.set(tYear, tMonth, tDate, tHour, tMin, tSec);
                if (tYear != tCalendarObj.get(Calendar.YEAR) || tMonth != (tCalendarObj.get(Calendar.MONTH))
                        || tDate != tCalendarObj.get(Calendar.DATE) || tHour != tCalendarObj.get(Calendar.HOUR_OF_DAY)
                        || tMin != tCalendarObj.get(Calendar.MINUTE) || tSec != tCalendarObj.get(Calendar.SECOND)) {
                    throw new Exception("��iήɶ���~!");
                }
            } else {
                throw new Exception("�ǤJ��׿�~!");
            }
        } catch (Exception e) {
            throw new Exception("string2Calendar():�ǤJ��nɶ����X�k,�L�k�ഫCalendar!" + e.getMessage());
        }
        return tCalendarObj;
    }

    /**
     * 去掉字符串的首尾空格,如果为null则置为"".
     * 
     * @param aInStr:
     *            输入字符串
     * @return
     */
    public synchronized static String cnvNullStr(String aInStr) {
        if (aInStr == null) {
            aInStr = "";
        } else {
            aInStr = aInStr.trim();
        }
        return aInStr;
    }

    /**
     * ASCII ==> EBCDIC
     * 
     * @param aBarrAscii
     * @return �
     */
    public static synchronized byte[] cnvASCII2EBCDIC(byte[] aBarrAscii) throws UnsupportedEncodingException {
        String tAsciiStr = new String(aBarrAscii);
        return tAsciiStr.getBytes("IBM-937");
    }

    /**
     * ASCII ==> EBCDIC
     * 
     * @param aAsciiStr
     * @return �
     */
    public static synchronized byte[] cnvASCII2EBCDIC(String aAsciiStr) throws UnsupportedEncodingException {
        return aAsciiStr.getBytes("IBM-937");
    }

    /**
     * EBCDIC ==> ASCII
     * 
     * @param aBarrEBCDIC
     * @return �
     */
    public static synchronized byte[] cnvEBCDIC2ASCII(byte[] aBarrEBCDIC) throws UnsupportedEncodingException {
        String tAsciiStr = new String(aBarrEBCDIC, "IBM-937");
        return tAsciiStr.getBytes();
    }

    /**
     * byte ==> HEX
     * 
     * @param aByteData
     * @return �
     */
    public static synchronized String bytetoString(byte[] aByteData) {
        String tTmp = "";
        String tStr = "";
        if (aByteData == null) {
            return "00";
        }
        for (int i = 0; i < aByteData.length; i++) {
            Byte tByte = new Byte(aByteData[i]);
            tTmp = Integer.toHexString(tByte.intValue());
            if (tTmp.length() < 2) {
                tTmp = "0" + tTmp;
            } else if (tTmp.length() > 2) {
                tTmp = tTmp.substring(tTmp.length() - 2);
            }
            tStr = tStr + tTmp;
        }
        return tStr.toUpperCase();
    }

    /**
     * 字符串转十六进制字节数组
     * 
     * @param aIn:
     *            传入的字符串
     * @return �
     */
    public static synchronized byte[] pack(String aIn) {
        byte[] tByteData = aIn.getBytes();
        byte[] tHexByte = new byte[tByteData.length / 2];
        int tIndex = 0;
        int tDigit2 = 0;
        int tDigit = 0;
        int tTmp = 0;

        for (int i = 0; i < tByteData.length; i = i + 2) {
            if (tByteData[i] > 64) {
                tDigit2 = (tByteData[i] - 55) << 4;
            } else {
                tDigit2 = (tByteData[i] - 48) << 4;
            }
            if (tByteData[i + 1] > 64) {
                tDigit = (tByteData[i + 1] - 55);
            } else {
                tDigit = (tByteData[i + 1] - 48);
            }
            tTmp = tDigit + tDigit2;
            if (tTmp > 127) {
                tTmp = tTmp - 256;

            }
            tHexByte[tIndex] = (byte) tTmp;
            tIndex++;
        }
        return tHexByte;
    }

    /**
     * 判断今天是否为工作日
     * 
     * @param none
     * @return boolean
     */
    public static boolean isBusinessDay() {
        boolean tIsBusinessDay = true;
        int tToday = 0;
        Calendar tCalendar = Calendar.getInstance();
        tToday = tCalendar.get(Calendar.DAY_OF_WEEK);
        if (tToday == Calendar.SUNDAY || tToday == Calendar.SATURDAY) {
            tIsBusinessDay = false;
        }
        return tIsBusinessDay;
    }

    /**
     * 取得系统当天日期
     * 
     * @param none
     * @return String ������ yyyyMMdd
     * @exception none
     */
    static public String getToday() {
        Calendar tCalendar = Calendar.getInstance();
        int tYear = tCalendar.get(Calendar.YEAR);
        int tMonth = tCalendar.get(Calendar.MONTH)+1;
        int tDate = tCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return String.valueOf(tYear * 10000 + tMonth * 100 + tDate);
    }
    

    /**
     * 返回该日期月份的天数
     * 
     * @param aToday:
     *            传入的日期 ���
     * @return int �Ӥ�aToday对应的月的天数�Ѽ�
     * @exception none
     */
    static public int getMaxMonthDay(String aToday) {
        int tNow_yyyy = Integer.parseInt(aToday.substring(0, 4));
        int tNow_mm = Integer.parseInt(aToday.substring(4, 6)) - 1;
        // int tDate = Integer.parseInt(aToday.substring(6, 8));

        Calendar tCalendar = Calendar.getInstance();
        tCalendar.clear();
        tCalendar.set(Calendar.YEAR, tNow_yyyy);
        tCalendar.set(Calendar.MONTH, tNow_mm);

        return tCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 判断是否是当月最后一天
     * 
     * @param aCurrDate
     *            ����
     * @return boolean true: 是当月最后一天 false: �不是当月最后一天��
     * @exception SQLException
     */
    static public boolean isMaxMonthDay(String aCurrDate) throws SQLException {
        int tDay = 0;

        // Get tDay
        tDay = Integer.parseInt(aCurrDate.substring(6, 8));

        // Processing
        if (tDay == getMaxMonthDay(aCurrDate)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 输入日期yyyymmdd,返回星期几��
     * 
     * @param aToday:
     *            yyyymmdd ���
     * @return int 周一 :1 周二 :2 周三 :3 � 周四 :4 周五 :5 周六 :6 � 周日 :7
     */
    static public int getDayOfWeek(String aToday) {
        int tNow_yyyy = Integer.parseInt(aToday.substring(0, 4));
        int tNow_mm = Integer.parseInt(aToday.substring(4, 6)) - 1;
        int tDate = Integer.parseInt(aToday.substring(6, 8));

        Calendar tCalendar = Calendar.getInstance();
        tCalendar.clear();
        tCalendar.set(Calendar.YEAR, tNow_yyyy);
        tCalendar.set(Calendar.MONTH, tNow_mm);
        tCalendar.set(Calendar.DATE, tDate);

        return tCalendar.get(Calendar.DAY_OF_WEEK);
    }

    // 判断传入的字串日期格式是否合法
    // ex. 20011112 or 20011112151617 or 151617
    // ex.
    public synchronized static boolean checkDateTimeValidity(String dateString) {
        int year = 0, month = 0, date = 0, hour = 0, min = 0, sec = 0, myLen = 0;
        if (dateString == null) {
            return false;
        }
        myLen = dateString.length();
        if (myLen == 8 || myLen == 14) {
            year = Integer.parseInt(dateString.substring(0, 4));
            month = Integer.parseInt(dateString.substring(4, 6)) - 1;
            date = Integer.parseInt(dateString.substring(6, 8));
            if (myLen == 14) {
                dateString = dateString.substring(8);
            }
        }
        if (dateString.length() == 6) {
            hour = Integer.parseInt(dateString.substring(0, 2));
            min = Integer.parseInt(dateString.substring(2, 4));
            sec = Integer.parseInt(dateString.substring(4, 6));
        }
        Calendar calendarObj = Calendar.getInstance();
        if (myLen == 8) {
            calendarObj.set(year, month, date);
            if (year != calendarObj.get(Calendar.YEAR) || month != (calendarObj.get(Calendar.MONTH))
                    || date != calendarObj.get(Calendar.DATE)) {
                return false;
            }
        } else if (myLen == 6) {
            if (hour < 0 || hour >= 24 || min < 0 || min >= 60 || sec < 0 || sec >= 60) {
                return false;
            }
        } else if (myLen == 14) {
            calendarObj.set(year, month, date, hour, min, sec);
            if (year != calendarObj.get(Calendar.YEAR) || month != (calendarObj.get(Calendar.MONTH))
                    || date != calendarObj.get(Calendar.DATE) || hour != calendarObj.get(Calendar.HOUR_OF_DAY)
                    || min != calendarObj.get(Calendar.MINUTE) || sec != calendarObj.get(Calendar.SECOND)) {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }
    
    /**
     * 取得当前日期的String。
     * @return String 返回值为日期,格式自定义，需要符合标准，参考Java Doc “SimpleDateFormat”
     */
    public static String getCurrentDate(String aFormat) {
        String tDate = new SimpleDateFormat(aFormat).format(new java.util.Date(System.currentTimeMillis()));
        return tDate;
    }
    /**
     * 格式化创建开始和接收日期
     * 
     * @param aStr
     *            存放日期数组对象
     * @return
     * @throws GSException
     */
    public static String[] formateDate(String[] aStr) throws GSException {
        String[] tStr = new String[2];
        // 判断页面传入的订单创建最小时间和最大时间值是否为空货或空字符
        if (aStr[0] == null || aStr[0].equals("")) {
            tStr[0] = "00000000";
        } else {
            if (aStr[0].length() != 10 && aStr[0].length() != 8) {
                GSLogWriter.debug(aStr[0]);
                throw new GSException(GSException.E00004, GSException.E00004_MSG);

            } else if (aStr[0].length() == 10)
                // 格式化成yyyymmdd
                tStr[0] = aStr[0].substring(0, 4) + aStr[0].substring(5, 7) + aStr[0].substring(8, 10);
            else
                tStr[0] = aStr[0];
        }
        if (aStr[1] == null || aStr[1].equals("")) {
            tStr[1] = "99999999";
        } else {
            if (aStr[1].length() != 10 && aStr[1].length() != 8) {
                GSLogWriter.debug(aStr[1]);
                throw new GSException(GSException.E00004, GSException.E00004_MSG);
            }
            if (aStr[1].length() == 10)
                // 格式化成yyyymmdd
                tStr[1] = aStr[1].substring(0, 4) + aStr[1].substring(5, 7) + aStr[1].substring(8, 10);
            else
                tStr[1] = aStr[1];
        }
        return tStr;
    }
    /**
     * 取得系统日期后7天的日期
     * @return
     * @throws Exception
     */
    public static String getNext7Day() throws Exception{
        String tRs = "";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal.add(Calendar.DATE,7);       //加7天
        tRs = sdf.format(cal.getTime()); //执行格式化
        return  tRs;
    }
    /**
     * 取得系统日期后7天的日期
     * @return
     * @throws Exception
     */
    public static String getTomorrow() throws Exception{
        String tRs = "";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal.add(Calendar.DATE,1);       //加1天
        tRs = sdf.format(cal.getTime()); //执行格式化
        return  tRs;
    }
    /**
     * 取得系统日期前1天的日期
     * @return
     * @throws Exception
     */
    public static String getYesterDay() throws Exception{
        String tRs = "";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        cal.add(Calendar.DATE,-1);       //-1天
        tRs = sdf.format(cal.getTime()); //执行格式化
        return  tRs;
    }
    /**
     * 取得系统日期某天的日期
     * @return
     * @throws Exception
     */
    public static String getNextUpDay(int aDay) throws Exception{
        String tRs = "";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        cal.add(Calendar.DATE,aDay);       //加7天
        tRs = sdf.format(cal.getTime()); //执行格式化
        return  tRs;
    }
    /**
     * 取得特定的某一天
     * @param aDay
     * @return
     * @throws Exception
     */
    public static String getSpecDay(int aDay) throws Exception{
        String tRs = "";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        cal.set(Calendar.DATE,aDay);       
        tRs = sdf.format(cal.getTime()); //执行格式化
        return  tRs;
        
    }
    /**
     * 取得某年月的最大天数
     * @param year
     * @param month
     * @return
     * @throws Exception
     */
    public static int getMaxDayOfMonth(int year,int month) throws Exception{
        Calendar tCalendar = Calendar.getInstance();
        tCalendar.clear();
        tCalendar.set(Calendar.YEAR, year);
        tCalendar.set(Calendar.MONTH, month - 1);

        return tCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    }
    /**
     * 格式化日期
     * @param aDate
     * @return
     * @throws Exception
     */
    public static String formatDate(String aDate) throws Exception{
        String tRs = "";
        if(aDate.length() != 10){
            throw new Exception("日期长度不符");
        }
        tRs = aDate.substring(0,4) + "-" + aDate.substring(5,7) + "-" + aDate.substring(8,10);
        return tRs;
    }
    /**
     * 去掉日期格式符号
     * @param aDate
     * @return
     * @throws Exception
     */
    public static String splitSpecChar(String aDate) throws Exception{
        String tRs = "";
        if(aDate.length() != 10){
            throw new Exception("日期长度不符");
        }
        tRs = aDate.substring(0,4) + aDate.substring(5,7) + aDate.substring(8,10);
        return tRs;
    }
    /**
     * 取得指定年月日及指定格式的日期
     * @param year      年
     * @param month     月
     * @param day       日
     * @param format    格式
     * @return
     */
    public static String getSpecDate(int year,int month,int day,String format){
        String tRs = "";
        Calendar tCal = Calendar.getInstance();
        tCal.set(Calendar.YEAR,year);
        tCal.set(Calendar.MONTH,month-1);
        tCal.set(Calendar.DATE,day);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        tRs = sdf.format(tCal.getTime());
        return tRs;
    }
    
    

    public static void main(String arg[]){
        
        System.out.println(System.getProperty("file.separator"));
  
        /*
    	try{
            String x = DateUtil.getNext7Day();
    		System.out.println(x);
            x = DateUtil.getCurrentDate("yyyy-MM-dd hh:mm:ss");
            System.out.println(x);
            int y = DateUtil.getMaxMonthDay(x);
            System.out.println(y);
            x = DateUtil.getSpecDay(30);
            System.out.println(x);
            y = DateUtil.getMaxDayOfMonth(Calendar.YEAR,2);
            System.out.println(y);
    	}catch(Exception e){
    		e.printStackTrace();
    	}*/
    }
    
}
